package com.usian.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;


@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Autowired
    private AdSensitiveMapper adSensitiveMapper;

    @Override
    public ResponseResult findNameByPage(SensitiveDto sensitiveDto) {
        if (sensitiveDto == null) {
            return ResponseResult.errorResult (AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        sensitiveDto.checkParam ();
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<> ();
        lambdaQueryWrapper.like (StringUtils.isNotEmpty (sensitiveDto.getName ()), AdSensitive::getSensitives, sensitiveDto.getName ());
        Page<AdSensitive> page = new Page<> (sensitiveDto.getPage (), sensitiveDto.getSize ());
        IPage<AdSensitive> iPage = this.page (page, lambdaQueryWrapper);
        PageResponseResult responseResult = new PageResponseResult (sensitiveDto.getPage (), sensitiveDto.getSize (), Integer.valueOf (iPage.getTotal () + ""));
        responseResult.setData (iPage.getRecords ());
        return responseResult;
    }

    @Override
    public ResponseResult deletes(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID);
        }
        this.removeByIds (Arrays.asList (ids));
        return ResponseResult.okResult (AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public void save1(AdSensitive adSensitive) {
        adSensitiveMapper.save1 (adSensitive);
    }


}
